/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-04-27     Administrator       the first version
 */
#include <DEVICE/device.h>
#include "string.h"
#include "stdlib.h"
#include "stdio.h"
#include <easyflash.h>

#define DBG_ENABLE
#define DBG_SECTION_NAME "control"
#define DBG_LEVEL DBG_LOG
#define DBG_COLOR
#include <rtdbg.h>

#include "MQTT/mqtt.h"
#include "GLOBAL/code.h"


// ---------------------------------红外---------------------------------------//
static void ctl_infrared_init() {
    rt_pin_mode(CTL_INFRARED_PIN_1, PIN_MODE_INPUT_PULLUP); // 上拉输入
    rt_pin_mode(CTL_INFRARED_PIN_1, PIN_MODE_INPUT_PULLUP); // 上拉输入
    infrared_arr[0].value = 0;
    infrared_arr[1].value = 0;
}

static int ctl_infaraed_get(int index)
{
    int value = 0;
    switch (index)
    {
    case 0:
        value = rt_pin_read(CTL_INFRARED_PIN_1);
        break;
    case 1:
        value = rt_pin_read(CTL_INFRARED_PIN_2);
        break;
    default:
        break;
    }
    if (value == 1)
    {
        return CTL_INFRARED_STATUS_ANYBODY;
    }
    else 
    {
        return CTL_INFRARED_STATUS_NOBODY;
    }
    return -1;
}

static void ctl_infrared_check(int index) {
    int value = ctl_infaraed_get(index);
    if (value != infrared_arr[index].value)
    {
        infrared_arr[index].value = value;
        ctl_upload_flag = 1;
    }
}



// ---------------------------------门禁---------------------------------------//
static void ctl_door_init() {
    rt_pin_mode(CTL_DOOR_PIN_1, PIN_MODE_INPUT_PULLUP); // 上拉输入
    rt_pin_mode(CTL_DOOR_PIN_2, PIN_MODE_INPUT_PULLUP); // 上拉输入
    door_arr[0].value = 0;
    door_arr[1].value = 0;
}

static int ctl_door_get(int index)
{
    int value = 0;
    switch (index)
    {
    case 0:
        value = rt_pin_read(CTL_DOOR_PIN_1);
        break;
    case 1:
        value = rt_pin_read(CTL_DOOR_PIN_2);
        break;
    default:
        break;
    }
    if (value == 1)
    {
        return CTL_DOOR_STATUS_OPEN;
    }
    else 
    {
        return CTL_DOOR_STATUS_CLOSE;
    }
    return -1;
}

static void ctl_door_check(int index) {
    int value = ctl_door_get(index);
    if (value != door_arr[index].value)
    {
        door_arr[index].value = value;
        ctl_upload_flag = 1;
    }
}


// ---------------------------------开关---------------------------------------//
static void ctl_switch_init() {
    rt_pin_mode(CTL_SWITCH_PIN_1, PIN_MODE_OUTPUT);
    rt_pin_mode(CTL_SWITCH_PIN_2, PIN_MODE_OUTPUT);

    switch_arr[0].value = 0;
    switch_arr[0].pin = CTL_SWITCH_PIN_1;

    switch_arr[1].value = 0;
    switch_arr[1].pin = CTL_SWITCH_PIN_2;
}

static void ctl_switch_set(int index, int value) {
    switch_arr[index].value = value;
    if (value == CTL_SWITCH_STATUS_OPEN) {
        rt_pin_write(switch_arr[index].pin, 1);
    }
    if (value == CTL_SWITCH_STATUS_CLOSE) {
        rt_pin_write(switch_arr[index].pin, 0);
    }
    ctl_upload_flag = 1;
}



// 自动化控制情况1： 红外有人  || 门开   ->   关闭源控
static void ctl_auto_1() {
    int is_run = 0;
    int i;
    // 红外
    for (i = 0; i < CTL_INFRARED_NUM; i++) //遍历最外层json键值对
    {
        // 有人
        if (infrared_arr[i].value == CTL_INFRARED_STATUS_ANYBODY) {
            is_run = 1;
            break;
        }
    }

    // 门禁
    for (i = 0; i < CTL_DOOR_NUM; i++) //遍历最外层json键值对
    {
        // 开启
        if (door_arr[i].value == CTL_DOOR_STATUS_OPEN) {
            is_run = 1;
            break;
        }
    }

    // 源控
    if (is_run) {
        // 开关0->关闭
        for (i = 0; i < CTL_SWITCH_NUM; i++) //遍历最外层json键值对
        {
            ctl_switch_set(i, CTL_SWITCH_STATUS_CLOSE);
        }
    }
}

static void ctl_check() {
    ctl_upload_flag = 0;

    int idx = 0;
    // 红外检测
    for (idx = 0; idx < CTL_INFRARED_NUM; idx++)
    {
        ctl_infrared_check(idx);
    }
    for (idx = 0; idx < CTL_DOOR_NUM; idx++)
    {
        ctl_door_check(idx);
    }

    // 自动化处理，当红外检测到有人，或者门禁检测到开启状态，关闭源控
    if (ctl_upload_flag == 1) {
        ctl_auto_1();
    }
}

void ctl_check_thread(void* paramenter)
{
    while (1)
    {
        ctl_check();
        rt_thread_mdelay(100);
    }
}


// --------------------------------------------------------------------------//
void ctl_init() {
    ctl_infrared_init();
    ctl_door_init();
    ctl_switch_init();

    // 开启检测线程
    rt_thread_t tid = RT_NULL;
    rt_err_t result;
    tid = rt_thread_create("ctl_dev", ctl_check_thread, RT_NULL, 1024, RT_THREAD_PRIORITY_MAX / 2, 2);
    if (tid != RT_NULL)
    {
        rt_thread_startup(tid);
    }
}